package com.qiangesoft.im.controller;

import com.qiangesoft.im.core.ImWebSocketServer;
import com.qiangesoft.im.entity.ImMessage;
import com.qiangesoft.im.pojo.bo.ImMessageBO;
import com.qiangesoft.im.pojo.dto.ImMessageDTO;
import com.qiangesoft.im.pojo.dto.ImMessageReadDTO;
import com.qiangesoft.im.pojo.dto.query.MessageQueryDTO;
import com.qiangesoft.im.pojo.dto.query.PageQueryDTO;
import com.qiangesoft.im.pojo.vo.ImMessageVO;
import com.qiangesoft.im.pojo.vo.PageResultVO;
import com.qiangesoft.im.pojo.vo.ResultInfo;
import com.qiangesoft.im.service.IImMessageReceiverService;
import com.qiangesoft.im.service.IImMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 群消息 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
@Api(tags = "消息")
@RestController
@RequestMapping("/im/message")
public class ImMessageController {

    @Autowired
    private IImMessageService messageService;
    @Autowired
    private IImMessageReceiverService messageReceiverService;

    @GetMapping
    @ApiOperation(value = "消息列表")
    public ResultInfo<PageResultVO<ImMessageVO>> listMessage(@Validated PageQueryDTO pageQuery, @Validated MessageQueryDTO messageQuery) {
        PageResultVO<ImMessageVO> pageResult = messageService.listMessage(pageQuery, messageQuery);
        return ResultInfo.ok(pageResult);
    }

    @GetMapping("/unread")
    @ApiOperation(value = "未读消息列表")
    public ResultInfo<List<ImMessageVO>> listUnreadMessage(Long chatId) {
        List<ImMessageVO> messageList = messageService.listUnreadMessage(chatId);
        return ResultInfo.ok(messageList);
    }

    @PostMapping("/send")
    @ApiOperation(value = "发送消息")
    public ResultInfo<Void> send(@Validated @RequestBody ImMessageDTO messageDTO) {
        ImMessage message = messageService.send(messageDTO);

        // 发送消息
        ImMessageBO messageBO = new ImMessageBO();
        messageBO.setId(message.getId());
        messageBO.setSenderId(message.getSenderId());
        messageBO.setChatId(message.getChatId());
        messageBO.setMessageType(message.getMessageType());
        messageBO.setMessage(message.getMessage());
        messageBO.setSendTime(message.getCreateTime());
        messageBO.setChatType(messageDTO.getChatType());
        messageBO.setTargetId(messageDTO.getTargetId());
        messageBO.setExtra(messageDTO.getExtra());
        messageBO.setTimestamp(messageDTO.getTimestamp());
        ImWebSocketServer.sendMessage(messageBO);
        return ResultInfo.ok();
    }

    @PutMapping("/read")
    @ApiOperation(value = "阅读消息")
    public ResultInfo<Void> read(@Validated @RequestBody ImMessageReadDTO messageReadDTO) {
        messageReceiverService.updateRead(messageReadDTO.getChatId(), messageReadDTO.getId());
        return ResultInfo.ok();
    }
}

